﻿using Unread.App.Views;
using Windows.UI.ApplicationSettings;
using Windows.UI.Core;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Media.Animation;

namespace Unread.App.Common
{
    /// <summary>
    /// Configures settings contract
    /// </summary>
    public class SettingsCharmInitializer
    {
        private const double SettingsWidth = 646;

        public Popup SettingsPopup { get; private set; }

        public void Initialize(SettingsPane sender, SettingsPaneCommandsRequestedEventArgs args)
        {
            InitializeLogoCredits(args);

            InitializeImportExportOpml(args);
        }

        private void InitializeLogoCredits(SettingsPaneCommandsRequestedEventArgs args)
        {
            var logoHandler = new UICommandInvokedHandler(cmd => ShowSettings(cmd, new LogoCreditPage()));
            var logoCreditCommand = new SettingsCommand("LogoCredits", "Logo", logoHandler);

            args.Request.ApplicationCommands.Add(logoCreditCommand);
        }

        private void InitializeImportExportOpml(SettingsPaneCommandsRequestedEventArgs args)
        {
            var opmlHandler = new UICommandInvokedHandler(cmd => ShowSettings(cmd, new OpmlPage()));
            var opmlCommand = new SettingsCommand("Opml", "Import / Export", opmlHandler);

            args.Request.ApplicationCommands.Add(opmlCommand);
        }

        private void ShowSettings(IUICommand command, Page page)
        {
            var windowBounds = Window.Current.Bounds;

            // Create popup
            SettingsPopup = new Popup
            {
                IsLightDismissEnabled = true,
                Width = SettingsWidth,
                Height = windowBounds.Height,
                ChildTransitions = new TransitionCollection()
            };

            // Hookup events
            SettingsPopup.Closed += (s, e) => Window.Current.Activated -= OnWindowActivated;
            Window.Current.Activated += OnWindowActivated;

            SettingsPopup.ChildTransitions.Add(new PaneThemeTransition
            {
                Edge = (SettingsPane.Edge == SettingsEdgeLocation.Right)
                    ? EdgeTransitionLocation.Right
                    : EdgeTransitionLocation.Left
            });

            // Set popup content
            page.Width = SettingsWidth;
            page.Height = windowBounds.Height;
            SettingsPopup.Child = page;

            // Show popup
            SettingsPopup.SetValue(Canvas.LeftProperty, SettingsPane.Edge == SettingsEdgeLocation.Right ? (windowBounds.Width - SettingsWidth) : 0);
            SettingsPopup.SetValue(Canvas.TopProperty, 0);
            SettingsPopup.IsOpen = true;
        }

        private void OnWindowActivated(object sender, Windows.UI.Core.WindowActivatedEventArgs e)
        {
            if (e.WindowActivationState == CoreWindowActivationState.Deactivated)
                if (SettingsPopup != null)
                    SettingsPopup.IsOpen = false;
        }
    }
}
